package com.cho.core.db;

import java.sql.Connection;
import java.sql.DriverManager;

import org.apache.log4j.Logger;

import com.cho.core.exception.DbConnectionException;
import com.cho.core.vo.ProjectVo;

public class OracleConnection {

	private static Logger logger =  Logger.getLogger("OracleConnection");

	static {
		try {
			Class.forName("oracle.jdbc.driver.OracleDriver");
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	public static Connection getConnection(ProjectVo vo) {
		Connection conn = null;

		//ProjectVo vo = new ProjectVo();

		try {
			
			logger.info("id:" + vo.getDbId());
			logger.info("id:" + vo.getDbPw());
			logger.info("id:" + vo.getDbPort());
			
			String url = "jdbc:oracle:thin:@" + vo.getDbHost() + ":" + vo.getDbPort() + ":" + vo.getDbServiceName();
//			String url = "jdbc:oracle:thin:@121.67.187.158:1521:orcl";
			Class.forName("oracle.jdbc.driver.OracleDriver");
			conn = DriverManager.getConnection(url, vo.getDbId(), vo.getDbPw());
			
		} catch (Exception e) {
			new DbConnectionException(vo);
		} 

		return conn;
	}
	
	public static String getConnectionTest(ProjectVo vo) {
		Connection conn = null;
		String connErrMsg = "";
		
		try {
			
			logger.info("toStringDbInfo:" + vo.toStringDbInfo());
			
			String url = "jdbc:oracle:thin:@" + vo.getDbHost() + ":" + vo.getDbPort() + ":" + vo.getDbServiceName();
//			String url = "jdbc:oracle:thin:@121.67.187.158:1521:orcl";
			Class.forName("oracle.jdbc.driver.OracleDriver");
			conn = DriverManager.getConnection(url, vo.getDbId(), vo.getDbPw());
			
		} catch (Exception e) {
			connErrMsg = e.getMessage();
		}finally{
			if(conn != null){try{conn.close();}catch(Exception e){}}
		}
		
		return connErrMsg;
	}

	public static void main(String[] args) {
		Connection conn = null;
		try{
			conn = OracleConnection.getConnection(new ProjectVo());
			System.out.println(conn);
		}finally{
		if(conn != null){try{conn.close();}catch(Exception e){}}
		}
	}
	/*

select * 
from  user_tab_comments
WHERE TABLE_TYPE = 'TABLE'


SELECT * FROM USER_TABLES


select * from dictionary
where table_name like '%TABLE%'

	 * 
	 * */
	/*
	 * public boolean open() { try { // 읽기 전용 SQLiteConfig config = new
	 * SQLiteConfig(); config.setReadOnly(true); this.connection =
	 * DriverManager.getConnection("jdbc:sqlite:/" + this.dbFileName,
	 * config.toProperties()); } catch(SQLException e) { e.printStackTrace();
	 * return false; }
	 * 
	 * isOpened = true; return true; }
	 * 
	 * 
	 * public boolean close() { if(this.isOpened == false) { return true; }
	 * 
	 * try { this.connection.close(); } catch(SQLException e) {
	 * e.printStackTrace(); return false; } return true; }
	 * 
	 * public boolean readMeta(String filePath, String hashCode, PCloudItem
	 * item) throws SQLException { if(this.isOpened == false) { return false; }
	 * 
	 * boolean result = false; String query =
	 * "SELECT * FROM media WHERE FilePath=? AND CheckSum=?;"; PreparedStatement
	 * prep = this.connection.prepareStatement(query); prep.setString(1,
	 * filePath); prep.setString(2, hashCode);
	 * 
	 * ResultSet row = prep.executeQuery(); if(row.next()) { row.getString(1);
	 * // index 로 가져오기 row.getString("FileSize"); // field 이름으로 가져오기
	 * 
	 * result = true; } row.close(); prep.close(); return result; }
	 */
}
